package com.dayuanit.booksystem.v2.service.impl;

import com.dayuanit.booksystem.v2.dao.BookDao;
import com.dayuanit.booksystem.v2.dao.impl.BookDaoImpl;
import com.dayuanit.booksystem.v2.domain.PrepareParamer;
import com.dayuanit.booksystem.v2.entity.Book;
import com.dayuanit.booksystem.v2.service.BookService;
import com.dayuanit.booksystem.v2.thread.ConnectionThread;
import com.dayuanit.booksystem.v2.util.DataBaseUtil;
import com.dayuanit.booksystem.v2.util.StringUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();

    @Override
    public void addBook(String bookName, String author, String price, long publisherId) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            bookDao.add(bookName, author, price, publisherId);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public Book queryBook(long bookId) {
        //先从连接池得到一个connection，
        Connection connection = DataBaseUtil.getConnection();

        try {//然后将其放到当前线程里，Dao层就可以从当前线程中得到此conection
            ConnectionThread.connThreadLocal.set(connection);
            return bookDao.getBookById(bookId);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }

    }

    @Override
    public Book queryBook(String bookName) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            return bookDao.getBookByName(bookName);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public List<Book> listBook(long publisherId) {

        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);
        try {
            return bookDao.listBook(publisherId);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public int countBook(long publisherId) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            return bookDao.count(publisherId);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public void upateBookPrice(long bookId, String newPrice) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            bookDao.updateBookPriceById(bookId, newPrice);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public void deleteBook(long bookId) {

    }

    @Override
    public void updateBookPrice(String bookName, String newPrice) {
        if (StringUtils.isBlank(bookName) || StringUtils.isBlank(newPrice)) {
            System.err.println("有必填参数为空");
            return;
        }

        Connection connection = DataBaseUtil.getConnection();
        try {
            ConnectionThread.connThreadLocal.set(connection);
            bookDao.updateBookPriceByName(bookName, newPrice);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }

    }
}
